package com.qf.base.controller;

import com.qf.base.domain.BaseDriver;
import com.qf.base.service.IBaseDriverService;
import com.qf.common.annotation.Anonymous;
import com.qf.common.annotation.Log;
import com.qf.common.core.controller.BaseController;
import com.qf.common.core.domain.AjaxResult;
import com.qf.common.core.page.TableDataInfo;
import com.qf.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * --- ONE PIECE ---
 *
 * @author yangjiangcheng
 * @date 2025/9/3
 * @desc
 */
@RestController
@RequestMapping("/tms/base/driver")
public class BaseDriverController extends BaseController {
    @Autowired
    private IBaseDriverService baseDriverService;

    /**
     * 获取司机信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseDriver driver) {
        startPage();
        List<BaseDriver> list = baseDriverService.selectDriverList(driver);
        return getDataTable(list);
    }


    /**
     * 新增司机信息
     */
    @Log(title = "司机管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BaseDriver driver) {
        driver.setCreateBy(getUsername());
        return toAjax(baseDriverService.insertDriver(driver));
    }


    /**
     * 根据司机Id获取详细信息
     */
    @GetMapping(value = "/{driverId}")
    public AjaxResult getInfo(@PathVariable Long driverId) {
        return success(baseDriverService.selectDriverById(driverId));
    }


    /**
     * 修改司机信息
     */
    @Log(title = "司机管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BaseDriver driver) {
        driver.setUpdateBy(getUsername());
        return toAjax(baseDriverService.updateDriver(driver));
    }


    /**
     * 删除司机信息
     */
    @Log(title = "司机管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(baseDriverService.deleteDriverByIds(ids));
    }

    // 根据承运商ID查询司机列表（供前端调用）
    @GetMapping("/carrier/{carrierId}")
    @Anonymous
    public AjaxResult getDriversByCarrierId(@PathVariable Long carrierId) {
        return AjaxResult.success(baseDriverService.getDriversByCarrierId(carrierId));
    }


}
